﻿using System.Collections.Generic;
using System.Linq;
using LogManager.Base;
using LogManager.Base.Data;
using LogManager.ViewModels;
using Microsoft.Practices.ServiceLocation;
using System;

namespace LogManager
{
    public class DataContext : LogManager.IDataContext 
    {
        public DataContext(IServerService serverService)
        {
            Servers = serverService;
        }

        public IServerService Servers { get; private set;}
        
        public virtual IEnumerable<ILogService> Logs
        {
            get
            {
                return Servers.All().Select(s => s.Logs);
            }
        }

        public virtual IEnumerable<IEventService> Events
        {
            get
            {
                return Logs.SelectMany(ls => ls.All().Select(l => l.Events));
            }
        }
        
        public virtual IEnumerable<IEvent> GetEvents()
        {
            return Events.SelectMany(es => es.WithSimpleType().All());
        }

        public virtual IEnumerable<ILog> GetLogs()
        {
            return Logs.SelectMany(es => es.All());
        }

        public virtual ILog GetLog(string serverName, string logName)
        {
            return Servers.Get(serverName).Logs.Get(logName);
        }
    }
}
